package com.ruoyi.xsyp.service.impl;

import java.util.List;

import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.xsyp.domain.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.xsyp.mapper.JianliMapper;
import com.ruoyi.xsyp.domain.Jianli;
import com.ruoyi.xsyp.service.IJianliService;
import com.ruoyi.common.core.text.Convert;

/**
 * 学生简历Service业务层处理
 * 
 * @author zhangxingui
 * @date 2022-06-10
 */
@Service
public class JianliServiceImpl implements IJianliService 
{
    @Autowired
    private JianliMapper jianliMapper;

    /**
     * 查询学生简历
     * 
     * @param jId 学生简历主键
     * @return 学生简历
     */
    @Override
    public Jianli selectJianliByJId(Long jId)
    {
        return jianliMapper.selectJianliByJId(jId);
    }

    /**
     * 查询学生简历列表
     * 
     * @param jianli 学生简历
     * @return 学生简历
     */
    @Override
    public List<Jianli> selectJianliList(Jianli jianli)
    {
        return jianliMapper.selectJianliList(jianli);
    }

    /**
     * 新增学生简历
     * 
     * @param jianli 学生简历
     * @return 结果
     */
    @Override
    public int insertJianli(Jianli jianli)
    {
        return jianliMapper.insertJianli(jianli);
    }

    /**
     * 修改学生简历
     * 
     * @param jianli 学生简历
     * @return 结果
     */
    @Override
    public int updateJianli(Jianli jianli)
    {
        return jianliMapper.updateJianli(jianli);
    }

    /**
     * 批量删除学生简历
     * 
     * @param jIds 需要删除的学生简历主键
     * @return 结果
     */
    @Override
    public int deleteJianliByJIds(String jIds)
    {
        return jianliMapper.deleteJianliByJIds(Convert.toStrArray(jIds));
    }

    /**
     * 删除学生简历信息
     * 
     * @param jId 学生简历主键
     * @return 结果
     */
    @Override
    public int deleteJianliByJId(Long jId)
    {
        return jianliMapper.deleteJianliByJId(jId);
    }

    @Override
    public String importJianli(List<Jianli> jianliList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(jianliList) || jianliList.size() == 0)
        {
            throw new BusinessException("导入学生数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        //String password = configService.selectConfigByKey("sys.user.initPassword");
        for (Jianli jianli : jianliList)
        {
            try
            {
                // 验证是否存在这个用户
                Jianli jl = jianliMapper.selectJianliByJId(jianli.getjId());
                if (StringUtils.isNull(jl))
                {
                    jianli.setCreateBy(operName);
                    this.insertJianli(jianli);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、简历信息 " + jl.getjName() + " 导入成功");
                }
                else if (updateSupport)
                {
                    jianli.setUpdateBy(operName);
                    this.updateJianli(jianli);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、简历信息 " + jianli.getjName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、简历信息 " + jianli.getjName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、简历信息 " + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new BusinessException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
